package com.boots.wx.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信支付配置
 */
@Configuration
@Data
@ConfigurationProperties(prefix = "wx.pay")
public class WxPayConfig {

    private String merchantId;
    private String privateKeyPath;
    private String merchantSerialNumber;
//    private String wechatPayCertificatePath;
    private String publicKeyPath;
    private String publicKeyId;
    private String apiV3Key;
    /**
     * 创建微信支付服务
     */
    @Bean
    public NativePayService WxPConfig() {
        // 可以根据实际情况使用publicKeyFromPath或publicKey加载公钥
        Config config =
                new RSAPublicKeyConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .publicKeyFromPath(publicKeyPath)
                        .publicKeyId(publicKeyId)
                        .apiV3Key(apiV3Key)
                        .build();

//        OkHttpClient client = new OkHttpClient.Builder()
//                .connectTimeout(10, TimeUnit.SECONDS)
//                .writeTimeout(10, TimeUnit.SECONDS)
//                .readTimeout(30, TimeUnit.SECONDS)
//                .build();
//
//        HttpClient httpClient =
//                new DefaultHttpClientBuilder()
//                        .config(config)
//                        .okHttpClient(client)
//                        .build();
        return new NativePayService.Builder().config(config).build();
    }

}
